/**
 * 
 */
package com.ekingstar.framework.security;

import org.springframework.security.Authentication;
import org.springframework.security.ConfigAttribute;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.vote.AccessDecisionVoter;

/**
 * 
 */
public class AdminVoter implements AccessDecisionVoter {

	/* (non-Javadoc)
	 * @see org.springframework.security.vote.AccessDecisionVoter#supports(org.springframework.security.ConfigAttribute)
	 */
	public boolean supports(ConfigAttribute attribute) {
		// TODO Auto-generated method stub
		return true;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.vote.AccessDecisionVoter#supports(java.lang.Class)
	 */
	public boolean supports(Class clazz) {
		// TODO Auto-generated method stub
		return true;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.vote.AccessDecisionVoter#vote(org.springframework.security.Authentication, java.lang.Object, org.springframework.security.ConfigAttributeDefinition)
	 */
	public int vote(Authentication authentication, Object object,
			ConfigAttributeDefinition config) {
		// TODO Auto-generated method stub
		boolean isSuperAdmin = UserUtil.isSuperAdmin(authentication.getName());
		if (isSuperAdmin){
			return AccessDecisionVoter.ACCESS_GRANTED;
		}
		/*GrantedAuthority[] gas = authentication.getAuthorities();
		if (gas != null ){
			for (int i =0 ;i < gas.length;i++){
				String auth = gas[i].getAuthority();
				if (auth != null && auth.trim().equals("ROLE_ADMINISTRATOR")){
					return AccessDecisionVoter.ACCESS_GRANTED;
				}
			}
		}*/
		
		return AccessDecisionVoter.ACCESS_ABSTAIN;
	}

}
